layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer;

    // 登录过期的时候，跳出ifram框架
    if (top.location != self.location) top.location = self.location;

    // 粒子线条背景
    $(document).ready(function () {
        $('.layui-container').particleground({
            dotColor: '#5cbdaa',
            lineColor: '#5cbdaa'
        });
    });

    // 进行登录操作
    form.on('submit(login)', function (data) {
        data = data.field;
        if (data.username == '') {
            layer.msg('用户名不能为空');
            return false;
        }
        if (data.password == '') {
            layer.msg('密码不能为空');
            return false;
        }
        if (data.captcha == '') {
            layer.msg('验证码不能为空');
            return false;
        }
        var info = new FormData();
        info.append('username', data.username);
        info.append('password', data.password);
        info.append('captcha', data.captcha);
        $.ajax({
            url: "/ask/action/login",
            type: "post",
            timeout: 3000,
            dataType: "json",
            contentType: 'application/x-www-form-urlencoded;charset=utf-8',
            data: info,
            contentType: false, //不设置内容类型
            processData: false, //不处理数据
            success: function (data) {
                if (data.code == 200) {
                    layer.msg(data.msg, {
                        time: 1000
                    }, function () {
                        window.location = '/' + data['data']['path'];
                    });
                } else {
                    layer.msg(data.msg);
                    $('#captchaPic').attr("src", $('#captchaPic').attr("src"));
                }
            },
            error: function (XMLResponse) {
                //异常捕获
                layer.msg(XMLResponse.responseText);
                $('#captchaPic').attr("src", $('#captchaPic').attr("src"));
            }
        });

        return false;
    });
});